using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// Transform扩展功能
/// </summary>
public static class TransformExtension
{
    /// <summary>
    /// 查找指定名字的子物体(包括未激活)
    /// </summary>
    /// <param name="transform"></param>
    /// <param name="name"></param>
    /// <returns>返回查找到的子物体，没找到返回null</returns>
    public static Transform ExFindChild(this Transform transform, string name)
    {
        foreach (Transform t in transform.GetComponentsInChildren<Transform>(true))
        {
            if (t.name == name)
            {
                return t;
            }
        }
        return null;
    }
    /// <summary>
    /// 查找所有符合指定名字的子物体(包括未激活)
    /// </summary>
    /// <param name="transform"></param>
    /// <param name="name"></param>
    /// <returns>返回所有查找到的子物体</returns>
    public static Transform[] ExFindChilds(this Transform transform, string name)
    {
        List<Transform> children = new List<Transform>();
        foreach (Transform t in transform.GetComponentsInChildren<Transform>(true))
        {
            if (t.name == name)
            {
                children.Add(t);
            }
        }
        return children.ToArray();
    }
}